package com.fengba.nanzhi.tabbar02;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 89311 on 2018/4/19.
 */

public class ZDAdapter extends RecyclerView.Adapter<ZDAdapter.LinerViewHolder> {

    private final Context context;
    private final JSONArray jsob;
    private JSONObject job;

    public ZDAdapter(Context context, JSONArray jsob) {
        this.context = context;
        this.jsob = jsob;
    }

    @Override
    public ZDAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ZDAdapter.LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.wz_activity_view,parent,false));
    }

    @Override
    public void onBindViewHolder(final LinerViewHolder holder, final int position) {
        try {
            job = (JSONObject) jsob.get(position);
            Glide.with(context).load(job.get("image").toString()).into(holder.mBtn);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        holder.mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    job = (JSONObject) jsob.get(position);
                    Intent intent = new Intent(context,GameActivity.class);
                    intent.putExtra("game_id",job.get("id").toString());
                     intent.putExtra("user_id","339");
                     context.startActivity(intent);
                 //   Tabbar02.tiaozhuan(job.get("id").toString(),"339");
//
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if(jsob.length()>0){
            return jsob.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private ImageButton mBtn;

        public LinerViewHolder(View itemView) {
            super(itemView);
            mBtn = itemView.findViewById(R.id.ZDBtn);
        }

    }
}
